package com.cloud.animationtestdemo.strage

import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.view.View

/**
 * @author: njb
 * @date:   2025/1/1 23:34
 * @desc:   描述
 */
class AlphaAnimationStrategy :BaseAnimationStrategy{
    override fun getAlphaForProgress(progress: Float): Float {
        // 直接返回进度对应的透明度，范围是0到1
        return progress
    }

    fun animateView(view: View, startAlpha: Float, endAlpha: Float, duration: Long) {
        val alphaProperty = PropertyValuesHolder.ofFloat("alpha", startAlpha, endAlpha)

        val animator = ValueAnimator.ofPropertyValuesHolder(alphaProperty)
        animator.duration = duration
        animator.addUpdateListener { animation ->
            val currentAlpha = (animation as? ValueAnimator)?.getAnimatedValue(alphaProperty.toString()) as? Float?: 0f
            view.alpha = currentAlpha
            val progress = animation.animatedFraction
            val alpha = getAlphaForProgress(progress)
            view.alpha = alpha
        }
        animator.start()
    }
}